import { replaceImageDomains, replaceImageDomainsBatch, getImageBaseUrl } from './configHelper'

// 示例数据
const sampleAppData = {
  id: "example_app_123",
  name: "Example App",
  icon: "https://www.doitme.link/cdn-cgi/image/icon/example.jpg",
  bigIcon: "https://www.doitme.link/cdn-cgi/image/anim=true,h=637,,f=webp,fit=cover/icon/example_big.jpg",
  screenshots: [
    "https://www.doitme.link/cdn-cgi/image/anim=true,w=300,h=200,,f=webp,fit=cover/screenshots/screen1.jpg",
    "https://www.doitme.link/cdn-cgi/image/anim=true,w=300,h=200,,f=webp,fit=cover/screenshots/screen2.jpg"
  ],
  features: "这是一个示例应用",
  download_url: "https://example.com/download"
}

// 示例1: 替换单个数据对象的图片域名
export function exampleReplaceSingleData() {
  console.log('原始数据:', sampleAppData)
  
  // 替换为 apkstem.com 域名的配置
  const processedData = replaceImageDomains(sampleAppData, 'apkstem.com')
  console.log('处理后的数据 (apkstem.com):', processedData)
  
  // 替换为默认域名的配置
  const processedDataDefault = replaceImageDomains(sampleAppData)
  console.log('处理后的数据 (默认域名):', processedDataDefault)
}

// 示例2: 批量替换多个数据对象
export function exampleReplaceBatchData() {
  const dataArray = [
    {
      id: "app1",
      icon: "https://www.doitme.link/cdn-cgi/image/icon/app1.jpg",
      bigIcon: "https://www.doitme.link/cdn-cgi/image/anim=true,h=637,,f=webp,fit=cover/icon/app1_big.jpg"
    },
    {
      id: "app2", 
      icon: "https://www.doitme.link/cdn-cgi/image/icon/app2.jpg",
      bigIcon: "https://www.doitme.link/cdn-cgi/image/anim=true,h=637,,f=webp,fit=cover/icon/app2_big.jpg"
    }
  ]
  
  console.log('原始数据数组:', dataArray)
  
  // 批量替换为指定域名
  const processedArray = replaceImageDomainsBatch(dataArray, 'apkstem.com')
  console.log('批量处理后的数据数组:', processedArray)
}

// 示例3: 获取图片基础URL
export function exampleGetImageBaseUrl() {
  // 获取指定域名的图片基础URL
  const apkstemBaseUrl = getImageBaseUrl('apkstem.com')
  console.log('apkstem.com 的图片基础URL:', apkstemBaseUrl)
  
  // 获取默认域名的图片基础URL
  const defaultBaseUrl = getImageBaseUrl()
  console.log('默认域名的图片基础URL:', defaultBaseUrl)
}

// 示例4: 在组件中使用
export function exampleUsageInComponent() {
  // 模拟从API获取的数据
  const apiData = {
    apps: [
      {
        id: "api_app_1",
        name: "API App 1",
        icon: "https://www.doitme.link/cdn-cgi/image/icon/api1.jpg",
        description: "这是一个API应用"
      },
      {
        id: "api_app_2",
        name: "API App 2", 
        icon: "https://www.doitme.link/cdn-cgi/image/icon/api2.jpg",
        description: "这是另一个API应用"
      }
    ],
    total: 2
  }
  
  // 根据当前域名替换图片URL
  const currentHostname = 'apkstem.com' // 这里应该是动态获取的
  const processedApiData = replaceImageDomains(apiData, currentHostname)
  
  console.log('API数据处理前:', apiData)
  console.log('API数据处理后:', processedApiData)
  
  return processedApiData
}

// 示例5: 处理嵌套数据结构
export function exampleNestedDataStructure() {
  const nestedData = {
    category: "AI Tools",
    subcategories: [
      {
        name: "Video Generation",
        apps: [
          {
            id: "video_app_1",
            icon: "https://www.doitme.link/cdn-cgi/image/icon/video1.jpg",
            screenshots: [
              "https://www.doitme.link/cdn-cgi/image/anim=true,w=300,h=200,,f=webp,fit=cover/screenshots/video1_screen1.jpg"
            ]
          }
        ]
      }
    ],
    metadata: {
      banner: "https://www.doitme.link/cdn-cgi/image/anim=true,w=800,h=200,,f=webp,fit=cover/banners/video_banner.jpg"
    }
  }
  
  console.log('嵌套数据结构处理前:', nestedData)
  
  // 处理嵌套数据
  const processedNestedData = replaceImageDomains(nestedData, 'apkstem.com')
  console.log('嵌套数据结构处理后:', processedNestedData)
  
  return processedNestedData
}

// 导出所有示例函数
export const imageDomainExamples = {
  exampleReplaceSingleData,
  exampleReplaceBatchData,
  exampleGetImageBaseUrl,
  exampleUsageInComponent,
  exampleNestedDataStructure
}
